﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;

using mesRUpload.GenerateSFCWebservice;

namespace mesRUpload
{
    class MiConnRequest : InfoDeal
    {
        private MiFindCustomAndSfcDataServiceService m_MiFindService;
        private miFindCustomAndSfcData m_miFindCustomAndSfcData;
        private miFindCustomAndSfcDataResponse m_response;

        public MiConnRequest()
        {
            m_MiFindService = new MiFindCustomAndSfcDataServiceService();
            m_miFindCustomAndSfcData = new miFindCustomAndSfcData();
            m_response = new miFindCustomAndSfcDataResponse();
        }

        public override long SetConnectInfo(string WSDL, string userName, string password, string timeout)
        {
            Timing.start();

            int time = -1;
            try
            {
                NetworkCredential credential = new NetworkCredential(userName, password);

                m_MiFindService.Url = WSDL;
                m_MiFindService.Credentials = credential; // 用户凭证
                m_MiFindService.PreAuthenticate = true; // 获取或设置是否启用预身份验证

                if (int.TryParse(timeout, out time))
                {
                    m_MiFindService.Timeout = time;
                }
                else
                {
                    MESUpload.errorLogger.Error("超时时间设置异常");

                    return CodeCollection.DATA_PARSE_FAIL;
                }

                return CodeCollection.EXECUTE_SUCCESS;
            }
            catch (Exception e)
            {
                string str = "[webservice]Url=\"" + WSDL + "\" "
                       + "user=\"" + userName + "\" "
                       + "password=\"" + EnDecryptAPI.EnCrypt(password, "mesrupload") + "\" "
                       + "Timeout=\"" + time + "\"";

                string filemessage = "webservice" + "," + "用户信息," + Timing.spendTime()
                       + "," + Timing.end() + "," + Timing.spendTime() + "," + "设置异常,"
                       + Timing.exceptionMess(e) + "," + str;
                //MESUpload.mifindLogger.Warn(filemessage);
                MESUpload.errorLogger.Error(filemessage);

                return CodeCollection.WSDL_EXCEPTION;
            }
        }

        public long setupResponse(findCustomAndSfcDataRequest dataRequest, out int code
            , out string message, out string inventory, out string sfc)
        {

            int ret_code = -1;
            string ret_message = string.Empty;
            string ret_sfc = string.Empty;
            string ret_inventory = string.Empty;

            Timing.start();

            try
            {
                m_miFindCustomAndSfcData.FindCustomAndSfcDataRequest = dataRequest;
                m_response = m_MiFindService.miFindCustomAndSfcData(m_miFindCustomAndSfcData);

                ret_code = m_response.@return.code;
                ret_message = m_response.@return.message;
                ret_inventory = m_response.@return.inventory;
                ret_sfc = m_response.@return.sfc;

                code = ret_code;
                message = ret_message;
                inventory = ret_inventory;
                sfc = ret_sfc;


                string requestmessage = "{\"request\":{\"设备站点\":\"" + dataRequest.site
                       + "\",\"操作用户\":\"" + dataRequest.user
                       + "\",\"activityId\":\"" + dataRequest.activity
                       + "\",\"过站模式\":\"" + dataRequest.modeProcessSFC
                       + "\",\"裸电芯条码\":\"" + dataRequest.inventory
                       + "\",\"是否获取SFC号\":\"" + dataRequest.findSfcByInventory
                       + "\",\"工位\":\"" + dataRequest.operation
                       + "\",\"工位版本\":\"" + dataRequest.operationRevision
                       + "\",\"设备资源号\":\"" + dataRequest.resource
                       + "\"}}";
                string filemessage = dataRequest.inventory + "," + "生成响应," + Timing.startTime()
                    + "," + Timing.end() + "," + Timing.spendTime() + ","
                    + (code == 0 ? "事务正常" : "事务异常") + message
                    + "," + requestmessage
                    + "[响应]code=\"" + code + "\" 内容=\"" + message
                    + "\" 电芯码=\"" + inventory + "\" sfc=\"" + sfc + "\"";
                MESUpload.mifindLogger.Info(filemessage);
                MESUpload.infoLogger.Info(filemessage);


                return CodeCollection.EXECUTE_SUCCESS;
            }
            catch (Exception e)
            {
                code = ret_code;
                message = ret_message;
                inventory = ret_inventory;
                sfc = ret_sfc;

                string filemessage = dataRequest.inventory + "," + "生成响应," + Timing.startTime()
                    + "," + Timing.end() + "," + Timing.spendTime() + ","
                    + (code == 0 ? "事务正常," : "事务异常,") + Timing.exceptionMess(e);
                MESUpload.mifindLogger.Warn(filemessage);
                MESUpload.errorLogger.Error(filemessage);

                return CodeCollection.RESPONSE_FAIL;
            }
        }

    }
}
